package com.bdqn.wang.servlet;

import com.bdqn.wang.pojo.User;
import com.bdqn.wang.service.UserService;
import com.bdqn.wang.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;

@WebServlet(urlPatterns = {"/login"})
public class LoginServlet extends HttpServlet {
    UserService userService = new UserServiceImpl();


    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html;charset=UTF-8");

        try {
            String name = req.getParameter("name");
            String pwd = req.getParameter("pwd");
            int user=userService.kais(name,pwd);
            if (user>0){
                req.getSession().setAttribute("loginUser",user);
                req.getRequestDispatcher("/WEB-INF/index.jsp").forward(req,resp);
            }else{
                req.getRequestDispatcher("login.jsp").forward(req,resp);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();

        }
    }
}
